package binance.tag

class ProgressTagLib {
    def progressbar = { attrs ->
        def width = attrs.width
        def progress = attrs.progress

        out << "<table width='"+width+"' style='border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; border-color: #000000;'>"
        out << "<tr>"
        if (progress >= 100) {
            out << "<td style='background-color: 68cf68; font-size: 0pt;'>&nbsp;</td>"
        } else if (progress <= 0) {
            out << "<td style='font-size: 0pt;'>&nbsp;</td>"
        }else{
            out << "<td width='"+progress+"%' style='background-color: 68cf68; font-size: 0pt;'>&nbsp;</td>"
            out << "<td width='"+(100-progress)+"%' style='font-size: 0pt;'>&nbsp;</td>"
        }
        out << "</tr>"
        out << "</table>"
    }

    def investment_status = { attrs ->
        Integer width = Integer.parseInt(attrs.width)-14
        Integer height = Integer.parseInt(attrs.height)
        def neg_color = attrs.neg_color
        def pos_color = attrs.pos_color
        def acc = attrs.acc
        Integer target = attrs.target
        def value = attrs.value

        log.debug "00 value:$value, acc:$acc, target:$target"
        
        Double pixpc = 0
        if (target > 0) {
            pixpc = width/target;
        }
        
        Integer neg_width = Math.round(pixpc * acc);
        Integer pos_width = width - neg_width;


        out << "<table width='$width' height='"+height+"' style='border-width: 0px 1px 0px 1px; border-style: solid solid solid solid; border-color: #000000;'>"
        out << "<tr>"

        if (acc >= value) {
            if (target < acc) {
                if (value >= target) {
                    log.debug "rött"
                    out << "<td width='${width}' style='background-color: ${neg_color}; font-size: 0pt;'>&nbsp;</td>"
                }else if (value < target){
                    log.debug "rött grott"
                    out << "<td width='${width-Math.round(pixpc * (target-value))}' style='background-color: ${neg_color}; font-size: 0pt;'>&nbsp;</td>"
                    out << "<td width='${Math.round(pixpc * (target-value))}' style='background-color: #DDDDDD; font-size: 0pt;'>&nbsp;</td>"
                }
            }else if (target >= acc) {
                log.debug "rött grott grott"
                out << "<td width='${neg_width-Math.round(pixpc * (acc-value))}' style='background-color: ${neg_color}; font-size: 0pt;'>&nbsp;</td>"
                out << "<td width='${Math.round(pixpc * (acc-value))}' style='background-color: #DDDDDD; font-size: 0pt;'>&nbsp;</td>"
                out << "<td width='${pos_width}' style='background-color: #DDDDDD; font-size: 0pt;'>&nbsp;</td>"
            }
        }else if (acc < value){
            if (target <= value) {
                log.debug "rött grönt"
                out << "<td width='${neg_width}' style='background-color: ${neg_color}; font-size: 0pt;'>&nbsp;</td>"
                out << "<td width='${pos_width}' style='background-color: ${pos_color}; font-size: 0pt;'>&nbsp;</td>"
            }else if (target > value) {
                log.debug "rött grönt grott"
                out << "<td width='${neg_width}' style='background-color: ${neg_color}; font-size: 0pt;'>&nbsp;</td>"
                out << "<td width='${Math.round(pixpc * (value-acc))}' style='background-color: ${pos_color}; font-size: 0pt;'>&nbsp;</td>"
                out << "<td width='${pos_width-Math.round(pixpc * (value-acc))}' style='background-color: #DDDDDD; font-size: 0pt;'>&nbsp;</td>"
            }
        }

        out << "</tr>"
        out << "</table>"
    }
}
